package com.yuri.news.service.impl;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.yuri.news.entity.Classify;
import com.yuri.news.jpa.ClassifyRepository;
import com.yuri.news.service.ClassifyService;

@Service
public class ClassifyServiceImpl implements ClassifyService {
	// 依赖注入
	@Autowired
	ClassifyRepository classifyRepository;
	@Override
	public Classify saveClassify(Classify classify) {
		return classifyRepository.save(classify);
	}

	@Override
	public Boolean updateClassify(Classify classify) {
		Boolean flag = false;
		
		Optional<Classify> optional = classifyRepository.findById(classify.getId());
		if(optional.isPresent()) {
			if(classify.getUpClassify() == null) {
				classify.setUpClassify(optional.get().getUpClassify());
			}
			if(classify.getUpClassify() != null && classify.getUpClassify().getId().equals(classify.getId())) {
				classify.setUpClassify(optional.get().getUpClassify());
			}
			
			if(classify.getUpClassify() != null && -1 == classify.getUpClassify().getId() ) {
				classify.setUpClassify(null);
			}
			classifyRepository.save(classify);
			flag = true;
		}
		
		return flag;
	}

	@Override
	public Boolean deleteClassify(Long id, Boolean flage) {
		Boolean flag = false;
		Optional<Classify> optional = classifyRepository.findById(id);
		
		if(optional.isPresent()) {
			// 查询其所有子分类
			List<Classify> classifies = classifyRepository.findDowClassById(id);
			for(Classify classify : classifies) {
				classify.setUpClassify(optional.get().getUpClassify());
				classifyRepository.save(classify);
			}
			classifyRepository.deleteById(id);
			flag = true;
		}
		
		return flag;
	}

	@Override
	public Classify getClassifyById(Long id) {
		Classify classify = null;
		
		Optional<Classify> optional = classifyRepository.findById(id);
		
		if(optional.isPresent()) {
			classify = optional.get();
		}
		
		return classify;
	}

	@Override
	public Page<Classify> listClassByAllOne(Pageable page) {
		Page<Classify> classify = null;
		
		classify = classifyRepository.findByUpClassifyIsNull(page);
		
		return classify;
	}

	@Override
	public List<Classify> listDowClassify(Long id) {
		List<Classify> classifies = null;
		
		classifies = classifyRepository.findDowClassById(id);
		
		if(classifies != null && classifies.size() > 0) {
			return classifies;
		}else {
			return null;
		}
	}

	@Override
	public List<Classify> listClassByAllOne() {
		return classifyRepository.findByUpClassifyIsNull();
	}

	@Override
	public Page<Classify> listDowClassify(Long id, Pageable page) {
		return classifyRepository.findDowClassById(id, page);
	}

	@Override
	public List<Classify> listClassify() {
		return classifyRepository.findAll();
	}

	@Override
	public Page<Classify> listClassify(Pageable page) {
		return classifyRepository.findAll(page);
	}
}
